//
//  GoodsViewController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/7/11.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "GoodsOrderViewController.h"

#define TOPVIEW_HEIGHT 36
#define SLIDEVIEW_HEIGHT 3

@interface GoodsOrderViewController ()<UIScrollViewDelegate>
{
    UIView *_topView;
    UIView *_slideView;
    UIScrollView *_bottomScrollView;
    UIButton *_selectedButton;
}

@property(nonatomic,strong)NSArray *selectArray;

@property(nonatomic,strong) NSMutableArray *btnArray;

//控制器下标索引
@property(nonatomic)NSInteger scrollerIndex;

@end


@implementation GoodsOrderViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"商品订单";
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    [self buildGoodsOrderView];
}

- (void)buildGoodsOrderView
{
    CGFloat btnWidth = WIDTH/self.selectArray.count;
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0,10,WIDTH,TOPVIEW_HEIGHT)];
    _topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_topView];
    
    _slideView = [[UIView alloc] initWithFrame:CGRectMake(0,TOPVIEW_HEIGHT-SLIDEVIEW_HEIGHT, btnWidth,SLIDEVIEW_HEIGHT)];
    _slideView.backgroundColor = DEFAULT_COLOR;
    [_topView addSubview:_slideView];
    
    for (int i = 0; i < self.selectArray.count; ++i) {
        
        UIView *View = [[UIView alloc] initWithFrame:CGRectMake(btnWidth*i,0,btnWidth,TOPVIEW_HEIGHT-SLIDEVIEW_HEIGHT)];
        
        UIButton *btn = [[UIButton alloc] init];;
        [btn setTitle:[NSString stringWithFormat:@"%@",self.selectArray[i]] forState:UIControlStateNormal];
        [btn setFrame:CGRectMake(0,0,btnWidth,TOPVIEW_HEIGHT-SLIDEVIEW_HEIGHT)];
        
        btn.tag = i;
        
        btn.titleLabel.font = FONT(13);
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:RGB(52, 52, 52) forState:UIControlStateNormal];
        [btn setTitleColor:DEFAULT_COLOR forState:UIControlStateDisabled];
        
        [View addSubview:btn];
        [_topView addSubview:View];
        [self.btnArray addObject:btn];
        
        [btn addTarget:self action:@selector(topSelectButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
    CGRect  bottomScrollViewFrame = CGRectMake(0,TOPVIEW_HEIGHT+10,WIDTH,HEIGHT-TOPVIEW_HEIGHT-10);
    _bottomScrollView= [[UIScrollView alloc] initWithFrame:bottomScrollViewFrame];
    [self.view addSubview:_bottomScrollView];
    _bottomScrollView.contentSize = CGSizeMake(self.selectArray.count*WIDTH,0);
    _bottomScrollView.pagingEnabled = YES;
    _bottomScrollView.showsVerticalScrollIndicator = false;
    _bottomScrollView.showsHorizontalScrollIndicator = false;
    _bottomScrollView.bounces = false;
    _bottomScrollView.directionalLockEnabled = YES;
    _bottomScrollView.delegate =self;
    
    _scrollerIndex = 0;
    _bottomScrollView.contentOffset = CGPointMake(WIDTH*_scrollerIndex, 0);
    
    for (int i = 0; i < self.selectArray.count; ++i) {
        UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(i * WIDTH, 0,WIDTH,bottomScrollViewFrame.size.height)];
        bg.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
        [_bottomScrollView addSubview:bg];
    }
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //
    //    NSLog(@"_scrollerIndex %ld",_scrollerIndex);
    //    NSLog(@"%ld",(long)scrollView.contentOffset.x);
    //    NSLog(@" btnarr%ld",self.btnArray.count);
    
    CGRect frame = _slideView.frame;
    frame.origin.x = scrollView.contentOffset.x/self.selectArray.count;
    _slideView.frame = frame;
    int pageNum = scrollView.contentOffset.x / WIDTH;
    //滚动buttonLabel color
    for (UIButton * btn in self.btnArray) {
        if (btn.tag == pageNum ) {
            btn.enabled = false;
        }else{
            btn.enabled = YES;
        }
    }
}


- (void)topSelectButtonDidClick:(UIButton *)sender
{
    UIButton *button = sender;
    _selectedButton.enabled = YES;
    button.enabled = false;
    _selectedButton = button;
    
    [_bottomScrollView setContentOffset:CGPointMake(button.tag * WIDTH, 0) animated:YES];
}


- (NSArray *)selectArray
{
    if (!_selectArray) {
        _selectArray = @[@"全部",@"待付款",@"代发货",@"待收货"];
    }
    return _selectArray;
}


@end
